home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9892 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  59 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: A question on for loop
  5. Message-ID: <1996Mar14.041132.26035@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <4i77ca$161@ccshst05.cs.uoguelph.ca>
  11. Date: Thu, 14 Mar 1996 04:11:32 GMT
  12.  
  13. In article <4i77ca$161@ccshst05.cs.uoguelph.ca>, thay@uoguelph.ca (Toby K Hay) writes:
  14. >Phil Boyd (boydp@hdc-usa.com) wrote:
  15. >: > |>
  16. >: > |> for(i=0;i< 20; i++)
  17. >: > |> {
  18. >: > ...
  19. >: > |>   if(condition==FALSE)
  20. >: > |>   continue;
  21. >: > ...
  22. >: > |> }
  23. >
  24. >: OK, now for a newbie question just for my own edification:
  25. >: Inside the for loop - does "i" start off with the value 0 so that
  26. >: the loop has the potential to repeat up to 20 times (assuming that
  27. >: "condition" remains FALSE)?
  28. >
  29. >My novice's answer is yes, i starts off at 0 because the for statement 
  30. >(i=0;i<20;i++) has i++ meaning that i is incremented after the 
  31. >operation.  If it were (i=0;i<20;++i) then i would start at 1 and run up 
  32. >to 20.  Is this correct?
  33.  
  34. No. The ++i is still only done at the end of the loop, and i starts at
  35. a value of 0 due to the initial assignment. The best way to think of
  36. the looping code is as follows (temporarily forgetting about the
  37. condition check shown above):
  38.  
  39.     i = 0;
  40.     while ( i < 20 )
  41.     {
  42.         [some code here]
  43.         ...
  44.  
  45.         ++i;    /* Can be i++ without any difference */
  46.     }
  47.  
  48. The ++i can be i++ without any difference *in this case shown* since
  49. you are not relying on the side effect of the assignment.
  50.  
  51.                                 Z
  52.  
  53.  
  54. -- 
  55. -------------------------------------------------------------------------
  56. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  57. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  58. -------------------------------------------------------------------------
  59.